package guestbook;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreFailureException;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreTimeoutException;
import com.google.appengine.api.datastore.Entity;


@SuppressWarnings("serial")
public class DataLoad extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws IOException {
		
		DatastoreService datastore =
                DatastoreServiceFactory.getDatastoreService();

		PrintWriter o = resp.getWriter();
        
        Entity sensor = new Entity("SensorName");

        sensor.setProperty("date", new Date());
        sensor.setProperty("processed", false);
        
        Boolean hasError = false;
   
		@SuppressWarnings("unchecked")
		Enumeration<String> e = req.getParameterNames();
        while (e.hasMoreElements()) {
          String name = (String) e.nextElement();
          String reqParam = req.getParameter(name);
         
          if ( name.contains("Sens") ) {
        	  //must be of type String, pattern "SENSOR*"
        	  if ( reqParam.indexOf("SENSOR") == -1) {
        		  o.println("Err: Sensorname not found. "+reqParam);
        		  log.info("Err: Sensorname not found. "+reqParam);
        		  hasError = true;
        	  } else {
        		  sensor.setProperty(name, reqParam);
        		  o.println((name + ": " + reqParam));
        	  }
          } else
          if ( ( name.contains("Temp") ) ||
        	   ( name.contains("Dew")  ) ||
        	   ( name.contains("RelH") ) ||
        	   ( name.contains("Press"))  ) {
        	  //value must be of data type float
        	  
        	  try {
        		  double d = Double.valueOf(reqParam.trim()).doubleValue();
        		  sensor.setProperty(name, d);
        		  o.println((name + ": " + d));
        	  } catch (Exception ex) {
        		  hasError = true;
        		  o.println(name+" is not a floatig value." + ex.getMessage());
        		  log.info(name+" is not a floatig value." + ex.getMessage());
        		  }
          } else {
        	  o.println("Input Parameter: "+name+" nicht erkannt.");
        	  log.info("Input Parameter: "+name+" nicht erkannt.");
        	  hasError = true;
          }
        }
        
        if (! hasError) {
	        try {
	        	datastore.put(sensor);
	        } catch (DatastoreFailureException e1) {
	        	log.info("sorry, DB too busy! values lost. - " + e1.getMessage());
	        } catch (DatastoreTimeoutException e1) {
	        	log.info("Datastore Timeout! - "+ e1.getMessage());
	        }
        } else {
        	resp.sendError(501, "values wrong");
        	log.info("Wrong values posted!");
        }

	}
	
}